package com.ymatou.iapi.hunter.testcase;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.GetHunterSubordinateListRequest;
import com.ymatou.iapi.hunter.service.GetHunterSubordinateListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterRelation;
import com.ymttest.database.sqlwapper.hunter.HunterRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetHunterSubordinateList {
	
	private static GetHunterSubordinateListRequest getHunterSubordinateListRequest;
	private static GetHunterSubordinateListCall getHunterSubordinateListCall;
	HunterRelationWrapper wrapper = new HunterRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查看推手直属下级");
	}

	@Before
	public void caseUp() {
		getHunterSubordinateListRequest = new GetHunterSubordinateListRequest();
		getHunterSubordinateListCall = new GetHunterSubordinateListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHunterSubordinateListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(long parentHunterId, JSONObject object) throws JSONException {
		
		List<HunterRelation> actualResult = wrapper.selectSubordinateByHunterIdWithLimit(parentHunterId);
		HunterRelation hunterRelation = wrapper.selectByPrimaryKey(parentHunterId);
		
		Logger.verifyEquals(object.getInt("totalRecords"), wrapper.selectSubordinateByHunterId(parentHunterId).size(),
				"验证totalRecords");
		Logger.verifyEquals(object.getInt("childrenTotal"), hunterRelation.getChildren_total(),
				"验证childrenTotal");
		
		for(int i = 0; i < actualResult.size(); i++) {

			long hunterId = object.getJSONArray("userList")
					.getJSONObject(i).getLong("userId");
			
			Logger.comment("开始验证：" + hunterId);
			
			Logger.verifyEquals(hunterId, actualResult.get(i).getHunter_id(),
					"验证userId");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getString("userName"), actualResult.get(i).getHunter_name(),
					"验证userName");
			DataUtil.verifyDateEquals(object.getJSONArray("userList")
					.getJSONObject(i).getString("addTime"), actualResult.get(i).getAdd_time(),
					"验证addTime");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getInt("hunterLevel"), actualResult.get(i).getHunter_level(),
					"验证hunterLevel");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getInt("childrenTotal"),
					actualResult.get(i).getChildren_total() == null ? 0
							: actualResult.get(i).getChildren_total(),
					"验证childrenTotal");
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_001() {
		Logger.start(true, "查看推手下级");
		try {
			
			getHunterSubordinateListRequest.setHunterId(4321);
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(100);
			getHunterSubordinateListRequest.setSubordinateUserName("");
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("success", getHunterSubordinateListCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
			verifyDb(4321, getHunterSubordinateListCall.getResult());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_002() {
		Logger.start(true, "根据下级用户名查找下级");
		try {
			
			getHunterSubordinateListRequest.setHunterId(4321);
			getHunterSubordinateListRequest.setSubordinateUserName("autotest2qbwYj4");
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(10);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("success", getHunterSubordinateListCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelation relation = wrapper
					.selectSubordinateByHunterIdAndSubordinateName(4321L,
							"autotest2qbwYj4");

			Logger.verifyEquals(
					getHunterSubordinateListCall.getResult().getJSONArray("userList")
							.getJSONObject(0).getLong("userId"),
							relation.getHunter_id(), "验证userId");
			Logger.verifyEquals(
					getHunterSubordinateListCall.getResult().getJSONArray("userList")
							.getJSONObject(0).getString("userName"),
							relation.getHunter_name(), "验证userName");
			DataUtil.verifyDateEquals(
					getHunterSubordinateListCall.getResult().getJSONArray("userList")
							.getJSONObject(0).getString("addTime"),
							relation.getAdd_time(), "验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_003() {
		Logger.start(true, "hunter为0");
		try {
			
			long hunterId = 0;
			
			getHunterSubordinateListRequest.setHunterId(hunterId);
			getHunterSubordinateListRequest.setSubordinateUserName("");
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(10);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("hunterId:推手用户编号不能为空", getHunterSubordinateListCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_004() {
		Logger.start(true, "pageSize为0");
		try {
			
			long hunterId = 4321;
			
			getHunterSubordinateListRequest.setHunterId(hunterId);
			getHunterSubordinateListRequest.setSubordinateUserName("");
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(0);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("pageSize:每页显示的条数不能为空", getHunterSubordinateListCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_005() {
		Logger.start(true, "pageIndex为0");
		try {
			
			long hunterId = 4321;
			
			getHunterSubordinateListRequest.setHunterId(hunterId);
			getHunterSubordinateListRequest.setSubordinateUserName("");
			getHunterSubordinateListRequest.setPageIndex(0);
			getHunterSubordinateListRequest.setPageSize(10);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("pageIndex:当前页码不能为空", getHunterSubordinateListCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_006() {
		Logger.start(true, "根据下属用户编号查找下级");
		try {
			
			getHunterSubordinateListRequest.setHunterId(4321);
			getHunterSubordinateListRequest.setSubordinateUserId(100001L);
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(10);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("success", getHunterSubordinateListCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelation relation = wrapper
					.selectSubordinateByHunterIdAndSubordinateId(4321L, 100001L);

			Logger.verifyEquals(
					getHunterSubordinateListCall.getResult().getJSONArray("userList")
							.getJSONObject(0).getLong("userId"),
							relation.getHunter_id(), "验证userId");
			Logger.verifyEquals(
					getHunterSubordinateListCall.getResult().getJSONArray("userList")
							.getJSONObject(0).getString("userName"),
							relation.getHunter_name(), "验证userName");
			DataUtil.verifyDateEquals(
					getHunterSubordinateListCall.getResult().getJSONArray("userList")
							.getJSONObject(0).getString("addTime"),
							relation.getAdd_time(), "验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_007() {
		Logger.start(true, "根据非直接下属的用户编号查找下级");
		try {
			
			getHunterSubordinateListRequest.setHunterId(4321);
			getHunterSubordinateListRequest.setSubordinateUserId(3003L);
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(10);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("success", getHunterSubordinateListCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");

			Logger.verifyEquals(getHunterSubordinateListCall.getResult()
					.getJSONArray("userList").length(), 0, "验证size");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterSubordinateList_008() {
		Logger.start(true, "根据下属用户编号和错误的下属名查找下级");
		try {
			
			getHunterSubordinateListRequest.setHunterId(4321);
			
			getHunterSubordinateListRequest.setSubordinateUserId(100001L);
			getHunterSubordinateListRequest.setSubordinateUserName("bans");
			getHunterSubordinateListRequest.setPageIndex(1);
			getHunterSubordinateListRequest.setPageSize(10);
			
			getHunterSubordinateListCall.setData(getHunterSubordinateListRequest);
			getHunterSubordinateListCall.callService();
			
			Logger.verifyEquals("success", getHunterSubordinateListCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterSubordinateListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterSubordinateListCall.getResponseData().getBoolean("success"),
					"验证success");
			
			Logger.verifyEquals(getHunterSubordinateListCall.getResult()
					.getJSONArray("userList").length(), 0, "验证size");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
